# -*- coding: utf-8 -*-
import numpy as np
import pandas as pd
from scipy.stats import t, shapiro, kstest, uniform, anderson

def perform_anderson(x):
    # AD 检验分析白噪声误差（正态分布）
    A, _, _ = anderson(x, 'norm')
    # 用近似公式计算 p-value(AD test)
    
    AD = A*(1 + (.75/len(x)) + 2.25/(len(x)**2))

    if AD >= .6:
        p_ad = np.exp(1.2937 - 5.709*AD - .0186*(AD**2))
    elif AD >=.34:
        p_ad = np.exp(.9177 - 4.279*AD - 1.38*(AD**2))
    elif AD >.2:
        p_ad = 1 - np.exp(-8.318 + 42.796*AD - 59.938*(AD**2))
    else:
        p_ad = 1 - np.exp(-13.436 + 101.14*AD - 223.73*(AD**2))

    return p_ad
